package com.fmsz.physicalheadline.activitys.news;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.fmsz.physicalheadline.BR;
import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.adapter.news.RecycleAdapter;
import com.fmsz.physicalheadline.application.MyApplication;
import com.fmsz.physicalheadline.databinding.ActivityFocusSecTypeBinding;
import com.fmsz.physicalheadline.entity.news.discuss.DiscussComments;
import com.fmsz.physicalheadline.entity.news.discuss.DiscussEntity;
import com.fmsz.physicalheadline.entity.news.discuss.DiscussNumEntity;
import com.fmsz.physicalheadline.entity.news.focus.FlagEntity;
import com.fmsz.physicalheadline.entity.news.focus.ItemData;
import com.fmsz.physicalheadline.entity.news.focus.ItemDataEntity;
import com.fmsz.physicalheadline.entity.news.focus.ItemRecommendEntity;
import com.fmsz.physicalheadline.entity.news.focus.RecommendData;
import com.fmsz.physicalheadline.interfaces.HttpService;
import com.fmsz.physicalheadline.util.TitleViewBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FocusSecTypeActivity extends AppCompatActivity implements Callback<ItemDataEntity> {

    private String link;
    private ActivityFocusSecTypeBinding binding;
    private RecycleAdapter<Object> adapter;
    private HttpService httpService;
    private String dashboard_id;
    private String dashboard_data;
    private String dashboard_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_focus_sec_type);
        Intent intent = getIntent();
        link = intent.getStringExtra("link");
        dashboard_id = intent.getStringExtra("dashboard_id");
        dashboard_data = intent.getStringExtra("dashboard_data");
        dashboard_type = intent.getStringExtra("dashboard_type");
        initTitle();
        HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(DiscussNumEntity.class,Pair.create(R.layout.discuss_num,BR.discuss_num));
        map.put(RecommendData.RecsEntity.class,Pair.create(R.layout.news_rec_normal,BR.news_rec));
        map.put(DiscussComments.class,Pair.create(R.layout.discuss_item,BR.discuss));
        map.put(FlagEntity.class,Pair.create(R.layout.discuss_num,BR.flag));
        map.put(String.class,Pair.create(R.layout.news_type2_content,BR.content_data));
        map.put(ItemData.class,Pair.create(R.layout.news_content_click_item,BR.click_content));
        adapter = new RecycleAdapter<>(this,new ArrayList<>(),map);
        binding.recycleItemSec.setAdapter(adapter);
        httpService = ((MyApplication) getApplication()).getHttpService();
        httpService.getItemSecData(link).enqueue(this);
    }

    private void initTitle() {
        new TitleViewBuilder(this);
    }

    @Override
    public void onResponse(Call<ItemDataEntity> call, Response<ItemDataEntity> response) {
        if (response.body().getCode()==200) {
            ItemData data = response.body().getData();
            adapter.add(data);
            String text_content = data.getText_content();
            List<String> text_images = data.getText_images();
            Matcher matcher = Pattern.compile("\\$\\{lx-(\\w+)-?(\\d*)\\}").matcher(text_content);
            StringBuffer buffer = new StringBuffer("<style>img{max-width:100%}</style>");
            while(matcher.find()){
                switch (matcher.group(1)) {
                    case "br":
                        matcher.appendReplacement(buffer,"<br/>");
                        break;
                    case "image":
                        matcher.appendReplacement(buffer,String.format(Locale.getDefault(),"<img src=\"%s\"/>",text_images.get(Integer.parseInt(matcher.group(2)))));
                        Log.i("1620", "onResponse: "+text_images.get(Integer.parseInt(matcher.group(2))));
                        break;
                }
            }
            matcher.appendTail(buffer);
            adapter.add(buffer.toString());
            loadRecommendData();
        }
    }
    @Override
    public void onFailure(Call<ItemDataEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    //下载推荐列表数据
    private void loadRecommendData() {
        httpService.getRecommendData(link).enqueue(new Callback<ItemRecommendEntity>() {
            @Override
            public void onResponse(Call<ItemRecommendEntity> call, Response<ItemRecommendEntity> response) {
                if (response.body().getCode()==200) {
                    if (response.body().getData().getRecs()!=null) {
                        FlagEntity flagEntity = new FlagEntity("推荐阅读");
                        adapter.add(flagEntity);
                        adapter.addAll(response.body().getData().getRecs());
                        loadDiscussNumData();
                    }
                }
            }

            @Override
            public void onFailure(Call<ItemRecommendEntity> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(FocusSecTypeActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadDiscussNumData() {
        httpService.getDiscussNum(dashboard_type,dashboard_data).enqueue(new Callback<DiscussNumEntity>() {
            @Override
            public void onResponse(Call<DiscussNumEntity> call, Response<DiscussNumEntity> response) {
                DiscussNumEntity body = response.body();
                if (body !=null && body.getCode()==200) {
                    body.setData(body.getData()+"条评论");
                    adapter.add(body);
                    loadDiscussData();
                }
            }

            @Override
            public void onFailure(Call<DiscussNumEntity> call, Throwable t) {

            }
        });
    }

    //下载评论数据
    private void loadDiscussData() {
        httpService.getDiscussData(dashboard_id).enqueue(new Callback<DiscussEntity>() {
            @Override
            public void onResponse(Call<DiscussEntity> call, Response<DiscussEntity> response) {
                DiscussEntity body = response.body();
                if (body.getCode()==200) {
                    if (body.getData().getComments().size()>0) {
                        adapter.addAll(body.getData().getComments());
                    }
                }
            }

            @Override
            public void onFailure(Call<DiscussEntity> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(FocusSecTypeActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
